package com.app;

import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class AppContext {

	public static final String APP_CONTEXT_FILE = "classpath:/config/applicationContext.xml";
	public static final String API_CONTEXT_FILE = "classpath:/config/apiContext.xml";
	public static final String SPIRA_CONTEXT_FILE = "classpath:/config/spiraContext.xml";
	
	public static final String LOG4J_PROPERTIES_FILE = "src/main/resources/config/log4j.properties";
	
	private static ApplicationContext context;
	
	private static ContextProperties contextProperties;
	
	static{
		
		PropertyConfigurator.configure(LOG4J_PROPERTIES_FILE);
		
		String[] contextLocations = new String[] { APP_CONTEXT_FILE, API_CONTEXT_FILE, SPIRA_CONTEXT_FILE }; 
		context = new ClassPathXmlApplicationContext(contextLocations);
		
		contextProperties = getBean(ContextProperties.class);
	}
	
	private AppContext(){}
	
	public static <T> T getBean(Class<T> beanClass){
		T beanInstance = (T) context.getBean(beanClass);
		return beanInstance;
	}
	
	public static String getProperty(String name){

		return contextProperties.getProperty(name);
	}
	
	public static boolean containsProperty(String name){
		
		return contextProperties.containsProperty(name);
	}
}
